//
//  TTCustomHeaderView.m
//  TaoTuo
//
//  Created by apple on 2020/11/19.
//  Copyright © 2020 刘广. All rights reserved.
//

#import "TTCustomHeaderView.h"

@interface TTCustomHeaderView ()

@property (nonatomic, strong) UIImageView *imageView;
/// 预估价
@property (nonatomic, strong) UILabel *estimateLabel;
/// 价格
@property (nonatomic, strong) UILabel *priceLabel;
/// 信息
@property (nonatomic, strong) UILabel *infoLabel;
/// 发货时间
@property (nonatomic, strong) UILabel *sendTimeLabel;

/// 带范围的
@property (nonatomic, strong) NSMutableAttributedString *rangeAttr;

@end

@implementation TTCustomHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setupSubviews];
        
    }
    return self;
}

- (void)setCustomModel:(TTCustomConditionModel *)customModel
{
    _customModel = customModel;
    
//    NSString *urlStr = [customModel.CoverMapImg stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:customModel.CoverMapImg] placeholderImage:[UIImage imageNamed:@"f_placeholder_img"] options:SDWebImageRetryFailed];

    NSString *priceStr = [NSString stringWithFormat:@"¥%@~¥%@", customModel.PriceMin, customModel.PriceMax];
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:priceStr];
    NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:@"¥" options:NSRegularExpressionCaseInsensitive error:nil];
    [regular enumerateMatchesInString:priceStr options:NSMatchingReportCompletion range:NSMakeRange(0, priceStr.length) usingBlock:^(NSTextCheckingResult * _Nullable result, NSMatchingFlags flags, BOOL * _Nonnull stop) {
        
        [attr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:13] range:result.range];
        
    }];
    self.priceLabel.attributedText = attr;
    
    self.rangeAttr = attr;
    
    self.sendTimeLabel.text = [NSString stringWithFormat:@"预计%@-%@号可以发货", customModel.CycleMin, customModel.CycleMax];
    
    [self layoutIfNeeded];
    CGFloat h = self.infoLabel.bottom + 10;
    self.height = h > 130 ? h : 130;
}

- (void)setPrice:(NSString *)price
{
    _price = price;
    if (price.length == 0) {
        self.priceLabel.attributedText = self.rangeAttr;
        return;
    }
    NSString *priceStr = [NSString stringWithFormat:@"¥%@", price];
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:priceStr];
    [attr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:13] range:NSMakeRange(0, 1)];
    self.priceLabel.attributedText = attr;
}

- (void)setInfoString:(NSString *)infoString
{
    _infoString = infoString;
    
    self.infoLabel.text = infoString;
    
    [self layoutIfNeeded];
    CGFloat h = self.infoLabel.bottom + 10;
    self.height = h > 130 ? h : 130;
    
}

- (void)setupSubviews
{
    _imageView = [[UIImageView alloc] init];
    _imageView.layer.cornerRadius = 10;
    _imageView.layer.masksToBounds = YES;
    _imageView.layer.borderColor = UIColorHex(#E6E6E6).CGColor;
    _imageView.layer.borderWidth = 0.5;
    [self addSubview:_imageView];
    [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(15);
        make.width.height.mas_equalTo(100);
    }];
    
    _estimateLabel = [UILabel labelWithTitle:@"预估价：" color:UIColorHex(333333) fontSize:14];
    [self addSubview:_estimateLabel];
    [_estimateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.left.equalTo(self.imageView.mas_right).offset(10);
    }];
    
    _priceLabel = [UILabel labelWithTitle:@"" color:UIColorHex(#FF1F1F) font:[UIFont boldSystemFontOfSize:20]];
    [self addSubview:_priceLabel];
    [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.estimateLabel);
        make.left.equalTo(self.estimateLabel.mas_right);
    }];
    
    _sendTimeLabel = [UILabel labelWithTitle:@"" color:UIColorHex(999999) fontSize:13];
    [self addSubview:_sendTimeLabel];
    [_sendTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.estimateLabel);
        make.top.equalTo(self.estimateLabel.mas_bottom).offset(5);
    }];
    
    _infoLabel = [UILabel labelWithTitle:@"" color:UIColorHex(666666) fontSize:13];
    _infoLabel.numberOfLines = 0;
    [self addSubview:_infoLabel];
    [_infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.sendTimeLabel.mas_bottom).offset(5);
        make.left.equalTo(self.estimateLabel);
        make.right.equalTo(self.mas_right).offset(-10);
    }];
    
}

@end
